package com.test.mapper.enitites;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.lang.StringUtils;

import com.test.mapper.exceptions.DuplicatePropertyException;

public class EntityModel implements Serializable {

	/**
	 * Generated serial version UID.
	 */
	private static final long serialVersionUID = 68051751451141193L;

	private Integer clientId = null;

	private String entityName = null;

	private String tableName = null;

	private Collection<EntityProperty> properties = null;

	private EntityProperty primaryKey = null;

	public void addProperty(final EntityProperty property) {
		if (property == null || StringUtils.isEmpty(property.getPropertyName())) {
			throw new NullPointerException("Cannot add null or a property with empty name");
		}
		if (propertyPresent(property)) {
			throw new DuplicatePropertyException("Property with name " + property.getPropertyName() + " already exists");
		}
		if (properties == null) {
			properties = new ArrayList<EntityProperty>();
		}
		properties.add(property);
	}

	public void removeProperty(final EntityProperty property) {
		if (property == null) {
			throw new NullPointerException("Cannot remove null property from entity");
		}
		if (StringUtils.isEmpty(property.getPropertyName()) || property.getPropertyType() == null) {
			throw new NullPointerException("Cannot remove empty property");
		}
		if (properties != null && properties.size() > 0) {
			final Iterator<EntityProperty> it = properties.iterator();
			while (it.hasNext()) {
				final EntityProperty prop = it.next();
				if (prop.getPropertyName().trim().equalsIgnoreCase(property.getPropertyName().trim())
						&& prop.getPropertyType() == property.getPropertyType()) {
					it.remove();
					return;
				}
			}
		}
	}

	private boolean propertyPresent(EntityProperty property) {
		if (properties == null || properties.size() == 0) {
			return false;
		}
		for (EntityProperty prop : properties) {
			if (prop.getPropertyName().trim().equalsIgnoreCase(property.getPropertyName().trim())) {
				return true;
			}
		}
		return false;
	}

	public String getEntityName() {
		return entityName;
	}

	public void setEntityName(String entityName) {
		this.entityName = entityName;
	}

	public Collection<EntityProperty> getProperties() {
		return properties;
	}

	public void setProperties(Collection<EntityProperty> properties) {
		this.properties = properties;
	}

	public EntityProperty getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(EntityProperty primaryKey) {
		this.primaryKey = primaryKey;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public Integer getClientId() {
		return clientId;
	}

	public void setClientId(Integer clientId) {
		this.clientId = clientId;
	}
}
